from flask import Flask,jsonify,render_template,request,escape
from api import geocode,walking
import pandas as pd
key="2c5e812a00f3d177e589bd4162b15249"

app = Flask(__name__)

# 调用日志记录函数
def log_request(req:'flask_request',res:str)->None:
	with open('vsearch.log','a') as log:
		print(req.form,req.remote_addr,req.user_agent,res,file=log,sep='|')

# 进入网页的首页，数据返回shouye_xianshi.html
@app.route('/')
def shouye() ->'html':
	return render_template('homepage.html',the_title='智能步行导航')

@app.route('/index',methods=['POST'])
def login_page() -> 'html':
	return render_template('index.html',the_title ="智能步行导航")

@app.route('/shouye_xianshi',methods=['POST'])
def gongnengye() -> 'html':
	return render_template('shouye_xianshi.html',the_title ="智能步行导航")

# 点击“搜索”后，调用高德api——geocode,walking，数据返回gongnengye
@app.route('/ob',methods=['POST'])
def gongneng():
	address_origin = request.form['address_one']
	origin = geocode(key,address_origin)
	address_destination = request.form['address_two']
	destination = geocode(key,address_destination)
	
	results = walking(key,origin,destination)['route']['paths'][0]['steps']
	daohang = []
	for i in results:
		daohang.append(i['instruction'])
	return render_template('gongnengye.html',res=daohang,the_title='您的步行导航：',)



# 查看日志
@app.route('/viewlog')
def view_the_log() ->'html':
	contents = []
	with open('vsearch.log') as log:
		for line in log:
			contents.append([])
			for item in line.split('|'):
				contents[-1].append(escape(item))
	titles = ('Form Data','Remote_addr','User_agent','Results')
	return render_template('viewlog.html',
		the_title='View Log',
		the_row_titles=titles,
		the_data=contents,)

app.run(debug=True)